home *** CD-ROM | disk | FTP | other *** search
/ Software Vault: The Gold Collection / Software Vault - The Gold Collection (American Databankers) (1993).ISO / cdr37 / satsfaxt.zip / CCPUTILS.ZIP / CCDEL.C < prev    next >
C/C++ Source or Header  |  1990-01-30  |  12KB  |  285 lines

  1. /*---------------------------------------------------------------------------*
  2.  *                    CCDEL.C                     *
  3.  *---------------------------------------------------------------------------*
  4.  * This program allows the user to delete files from the Task and Log         *
  5.  * queues. Options allow for the deletion of all errors, individual         *
  6.  * jobs, or all jobs.                                 *
  7.  *                                         *
  8.  * The following source code is intended to assist developers in         *
  9.  * creating applications which support the  DCA/Intel Communicating         *
  10.  * Applications Specification Version 1.0A. It is provided free of charge    *
  11.  * and on an as-is basis. THE IMPLIED WARRENTIES OF MERCHANTABILITY AND      *
  12.  * FITNESS FOR A PARTICULAR PURPOSE ARE SPECIFICALLY EXCLUDED. This source   *
  13.  * code may be modified, enhanced, copied and distributed with applications  *
  14.  * that support CAS on a royalty free basis.                     *
  15.  *---------------------------------------------------------------------------*
  16.  * History: Initial release 9/14/89.                         *
  17.  *===========================================================================*/
  18. #include <stdio.h>
  19. #include <dos.h>
  20. #include <io.h>
  21. #include <malloc.h>
  22. #include "cas.h"        /* Intel CAS header file.             */
  23. #include "parse.h"        /* Command line parser header file.         */
  24. #include "util.h"        /* CAS utilities header file.             */
  25.  
  26. int AllFiles = FALSE;        /* Delete all control files.             */
  27. int AllErrors = FALSE;        /* Delete all control files with errors.     */
  28. char *JobNo = "0";        /* Delete a single job.              */
  29. unsigned long EventNo = 0;    /* Converted Job number.             */
  30. int Verbose = FALSE;        /* Echo job#'s as they are deleted.          */
  31. int Confirm = FALSE;        /* Confirm deletion of control files.         */
  32. int Help = FALSE;        /* Show help text.                 */
  33. int RedirOut = FALSE;        /* Redirect output to file.             */
  34. int EventDeleted = FALSE;    /* Set TRUE if event deleted.             */
  35.  
  36. ECF *TCFbuffer;         /* Task Control File buffer.             */
  37. FILE *ccout;            /* Redirected stdout file.             */
  38.  
  39. STABLE SwitchTable[] =        /* Command line parser switch table.         */
  40. {
  41.     { 'A', BOOL,    &AllFiles      },
  42.     { 'a', BOOL,    &AllFiles      },
  43.     { 'C', BOOL,    &Confirm      },
  44.     { 'c', BOOL,    &Confirm      },
  45.     { 'L', BOOL,    &AllErrors      },
  46.     { 'l', BOOL,    &AllErrors      },
  47.     { 'E', STRING,  (int *)&JobNo },
  48.     { 'e', STRING,  (int *)&JobNo },
  49.     { 'N', BOOL,    &Verbose      },
  50.     { 'n', BOOL,    &Verbose      },
  51.     { 'R', BOOL,    &RedirOut      },
  52.     { 'r', BOOL,    &RedirOut      },
  53.     { '?', BOOL,    &Help      }
  54. };
  55. #define TABLESIZE (sizeof(SwitchTable)/sizeof(STABLE))
  56.  
  57. char *DescTable[] =        /* Help function text table. */
  58. {
  59.     "CCDEL (V1.0) Deletes files from the receive and log queues.\n\n",
  60.     "The options let you delete errors (/L), individual events (/E),\n",
  61.     "or all (/A) files.\n\n",
  62.     "Syntax: CCDEL [option...]\n\n"
  63. };
  64. #define DESCSIZE (sizeof(DescTable)/sizeof(DescTable[0]))
  65.  
  66. char *HelpTable[] =        /* Help function text table. */
  67. {
  68.      "  /A           Delete all files from the receive and log queues.\n",
  69.      "  /E<number>   Delete all files associated with the event specified\n"
  70.      "               by <number>.\n",
  71.      "  /L           Delete all errors from the log queue.\n\n",
  72.      "  /C           Confirm before deletetion.\n",
  73.      "  /N           Notify by listing events as they're deleted.\n\n",
  74.      "  /R           Redirect output to file OUTPUT.CC.\n",
  75.      "  /?           Display help information.\n"
  76. };
  77. #define HELPSIZE (sizeof(HelpTable)/sizeof(HelpTable[0]))
  78.  
  79. /*---------------------------------------------------------------------------*
  80.  *                   GetTaskBuffer()                     *
  81.  *---------------------------------------------------------------------------*
  82.  * Get information about the specified task (Handle contains the job number).*
  83.  * On return, TCFbuffer is filled with the Task Conrol File information.     *
  84.  *---------------------------------------------------------------------------*
  85.  * Parameters:    int Handle - Event handle to return information from.         *
  86.  *        BYTE Queue - The queue (Task, Log or Receive) to return info *
  87.  *            from.                             *
  88.  *        char *TCFbuffer - Uninitialized ECF structure, filled with   *
  89.  *            task info on return.                     *
  90.  *        int *FileHandle - Filled with DOS file handle on return.     *
  91.  * Return:    none                                 *
  92.  *---------------------------------------------------------------------------*/
  93. GetTaskBuffer(int Handle, BYTE Queue, char *TCFbuffer, int *FileHandle)
  94. {
  95.  
  96.     /* Open the Task Control File and read in the TCF portion of it. */
  97.     if((*FileHandle = CASOpenFile(Handle, 0, Queue)) > 0) {
  98.         read(*FileHandle, TCFbuffer, sizeof(ECF));
  99.         }
  100.     else
  101.     CASError(CASNOTCF, TRUE, -(*FileHandle));
  102. }
  103.  
  104. /*---------------------------------------------------------------------------*
  105.  *                 DeleteAll()                     *
  106.  *---------------------------------------------------------------------------*
  107.  * Delete all entries that terminated due to errors. If confirmation asked   *
  108.  * from command line, query user before deleting. If verbose specified,      *
  109.  * notify user but don't querey before delete.                               *
  110.  *---------------------------------------------------------------------------*
  111.  * Paramters:    ECF *TCFbuffer - Uninitialized ECF structure.             *
  112.  *        BYTE Queue - Which queue to delete from - RECEIVE or LOG.    *
  113.  * Return:    none                                 *
  114.  *---------------------------------------------------------------------------*/
  115. int DeleteAll(ECF *TCFbuffer, BYTE Queue)
  116. {
  117.     int EventHandle;            /* Event handle from FindFirst/Next.     */
  118.  
  119.     /* Find the first event with errors. */
  120.     EventHandle = CASFindFirst(ANY_STATUS, SEARCH_FORWARD, Queue);
  121.  
  122.     while(EventHandle > 0) {
  123.  
  124.      /* Determine which events to delete. */
  125.      if(AllFiles)
  126.          DeleteJob(EventHandle, TCFbuffer, Queue);
  127.      else if(AllErrors)
  128.          DeleteError(EventHandle, TCFbuffer, Queue);
  129.  
  130.     /* Find the next error, additional parameters are remembered from the
  131.      * CASFindFirst call above. */
  132.     EventHandle = CASFindNext(Queue);
  133.     }
  134. }
  135.  
  136. /*----------------------------------------------------------------------------*
  137.  *                  DeleteJob()                      *
  138.  *----------------------------------------------------------------------------*
  139.  * Delete a specified job. The control file and all associated files are      *
  140.  * removed from the queue.                              *
  141.  *----------------------------------------------------------------------------*
  142.  * Parameters:    int EventHandle - Event handle to delete.              *
  143.  *        ECF *TCFbuffer - Uninitialized ECF structure.              *
  144.  * Return:    none                                  *
  145.  *----------------------------------------------------------------------------*/
  146. DeleteJob(int Event, ECF *TCFbuffer, BYTE Queue)
  147. {
  148.     int FileHandle;            /* DOS file handle (from GetTaskBuffer). */
  149.     char answer[1];            /* Users response to confirmation quest. */
  150.  
  151.     /* Get information about the Task. Don't forget to close the file. */
  152.     GetTaskBuffer(Event, Queue, (char *)TCFbuffer, &FileHandle);
  153.     close(FileHandle);
  154.  
  155.     /* If requested, be verbose/confirm request. */
  156.     if(Verbose | Confirm) {
  157.     EventDeleted = TRUE;
  158.     fprintf(stdout, "Event Number %d (%s queue entry).\n", Event, (Queue == 1) ? "Receive" : "Log");
  159.     if(Confirm) {
  160.         fprintf(stderr, "Delete event (y/n)? ");
  161.         gets(answer);
  162.     }
  163.         if((Confirm && (toupper(answer[0]) == 'Y')) || (!Confirm))
  164.         if(CASDeleteFile(Event, 0, Queue) == SUCCESS)
  165.         fprintf(stdout, "Job deleted.\n");
  166.         else
  167.         CASError(CASDELJOB, FALSE, 0);
  168.     }
  169.     else
  170.     if(CASDeleteFile(Event, 0, Queue) == SUCCESS)
  171.         EventDeleted = TRUE;
  172.     else
  173.         CASError(CASDELJOB, FALSE, 0);
  174. }
  175.  
  176. /*---------------------------------------------------------------------------*
  177.  *                 DeleteError()                     *
  178.  *---------------------------------------------------------------------------*
  179.  * Routine to delete errored events from the log  or receive queue.         *
  180.  * Information about each event is retrieved from the queue - if it         *
  181.  * is logged as an error or was aborted, then it is removed from the queue   *
  182.  * (user asked for confirmation if specified on the command line.         *
  183.  *---------------------------------------------------------------------------*
  184.  * Parameters:    int Event - Event handle to remove.                 *
  185.  *        ECF *TCFbuffer - Task control file information.          *
  186.  *        BYTE Queue - Which queue to delete from (LOG or RECEIVE).    *
  187.  *---------------------------------------------------------------------------*/
  188. DeleteError(int Event, ECF *TCFbuffer, BYTE Queue)
  189. {
  190.     int FileHandle;            /* DOS file handle (from GetTaskBuffer). */
  191.     char answer[1];            /* Users response from confirmation ques.*/
  192.  
  193.     /* Get information about the Task. Don't forget to close the file. */
  194.     GetTaskBuffer(Event, LOG_QUEUE, (char *)TCFbuffer, &FileHandle);
  195.     close(FileHandle);
  196.  
  197.     /* Less than COMPLETED is an error, aborted is not really an error but... */
  198.     if((TCFbuffer->EventStatus < COMPLETED) || (TCFbuffer->EventStatus == ABORTED)) {
  199.     if(Verbose | Confirm) {
  200.         EventDeleted = TRUE;
  201.         fprintf(stdout, "Event Number %d (%s queue entry).\n", Event, (Queue == 1) ? "Receive" : "Log");
  202.         if(Confirm) {
  203.         fprintf(stderr, "Delete event (y/n)? ");
  204.         gets(answer);
  205.         }
  206.         if((Confirm && (toupper(answer[0]) == 'Y')) || (!Confirm))
  207.         if(CASDeleteFile(Event, 0, Queue) == SUCCESS)
  208.             fprintf(stdout, "Event deleted.\n");
  209.         else
  210.             CASError(CASDELJOB, FALSE, 0);
  211.     }
  212.     else
  213.     if(CASDeleteFile(Event, 0, Queue) == SUCCESS)
  214.         EventDeleted = TRUE;
  215.     else
  216.         CASError(CASDELJOB, FALSE, 0);
  217.     }
  218. }
  219.  
  220. /*---------------------------------------------------------------------------*
  221.  *                   Main                      *
  222.  *---------------------------------------------------------------------------*
  223.  * Delete specified files from the Intel Connection CoProcessor queue. The   *
  224.  * normal use is to specify a job number with the /J switch. All files can   *
  225.  * be deleted using the /A switch and all errors with the /E switch.         *
  226.  *---------------------------------------------------------------------------*
  227.  * Return: The DOS exit code is 0 if no errors are encountered, 1 for         *
  228.  *       commandline syntax errors, 2 for CCAM error, and 3 for DOS         *
  229.  *       errors.                                 *
  230.  *---------------------------------------------------------------------------*/
  231. main(int argc, char **argv)
  232. {
  233.     int result;             /* DOS & CAS function return values.     */
  234.  
  235.     argc = ParseCommand(argc, argv, SwitchTable, TABLESIZE);
  236.  
  237.     /* Convert the event number to a long. */
  238.     EventNo = atol(JobNo);
  239.  
  240.     /* If help is wanted, print info and exit (with 0 exit code). */
  241.     if(Help || (argc < 1))
  242.     CASHelp(DescTable, DESCSIZE, HelpTable, HELPSIZE);
  243.  
  244.     /* Insure that the Resident Scheduler is installed. */
  245.     if((result = CASGetInstalledState()) != INSTALLED) {
  246.     if( result == NOTiOK )
  247.         CASError(CASNOHWO, TRUE, 0);
  248.     else
  249.         CASError(CASNOHWN, TRUE, 0);
  250.     }
  251.  
  252.     /* Redirect stdout to a file. */
  253.     if(RedirOut) {
  254.     if((ccout = fopen("OUTPUT.CC", "w")) == NULL)
  255.         CASError(CASOPENTEMP, TRUE, 0);
  256.     if(-1 == dup2(fileno(ccout), 1))
  257.         CASError(CASBADREDIR, TRUE, 0);
  258.     }
  259.  
  260.     /* Get buffer for Task information. */
  261.     TCFbuffer = (ECF *)malloc(sizeof(ECF));
  262.     if(TCFbuffer == NULL)
  263.     CASError(CASNOMEM, TRUE, 0);
  264.  
  265.     /* If job specified, delete it, otherwise delete all files or all
  266.      * errors depending on what switches were set on command line. */
  267.     if(EventNo > 0) {
  268.     DeleteJob(EventNo, TCFbuffer, LOG_QUEUE);
  269.     if(TCFbuffer->EventType == RECEIVE)
  270.         DeleteJob(EventNo, TCFbuffer, RECEIVE_QUEUE);
  271.     }
  272.     else if(AllFiles) {
  273.     DeleteAll(TCFbuffer, LOG_QUEUE);
  274.     if(TCFbuffer->EventType == RECEIVE)
  275.         DeleteAll(TCFbuffer, RECEIVE_QUEUE);
  276.     }
  277.     else if(AllErrors)
  278.     DeleteAll(TCFbuffer, LOG_QUEUE);
  279.  
  280.     if(!EventDeleted)
  281.     fprintf(stderr, "No event to delete.\n");
  282.  
  283.     exit(0);
  284. }
  285.